home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 12373 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  3.8 KB

  1. Path: news.clark.net!not-for-mail
  2. From: gusty@clark.net (Harlan Messinger)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Operator overload problem!
  5. Date: 19 Mar 1996 18:12:50 GMT
  6. Organization: Clark Internet Services, Inc., Ellicott City, MD USA
  7. Message-ID: <4imtf2$520@clarknet.clark.net>
  8. References: <DoIn1z.Gou@latcs1.lat.oz.au>
  9. NNTP-Posting-Host: explorer.clark.net
  10. Mime-Version: 1.0
  11. Content-Type: TEXT/PLAIN; charset=ISO-8859-1
  12. Content-Transfer-Encoding: 8bit
  13. X-Newsreader: TIN [UNIX 1.3 950726BETA PL0]
  14.  
  15. I'm not sure what's causing your particular syntax error messages, but
  16. your << overload function promises to return an ostream& and then doesn't
  17. return anything.
  18.  
  19. Did you #include <iostream.h>? 
  20.  
  21. Gregary J Boyles (boylesgj@lion.cs.latrobe.edu.au) wrote:
  22. : I am trying to overload the << but I get the syntax errors : friends must be functions or
  23. : classes, 'ostream' cannot start a parameter declaration, 'Address::operator <<(int &,Address &)'
  24. : must be declared at the first set of astericks and declaration syntax error at the second set.
  25. : What the #$%& is it on about? I copied the operator overload function directly out of a program
  26. : which compiles and runs so why all of a sudden won't the bloody compiler accept it?
  27. : #include "defines.h"
  28. : class Address
  29. : {
  30. :      private : int Number;
  31. :            char *Street;
  32. :            char *City;
  33. :            int Zip;
  34. :      public : // Constructors
  35. :           Address(int ANumber,const char *AStreet,const char *ACity,int Zip);
  36. :           Address();
  37. :           // Copy constructor
  38. :           Address(Address& AnAddress);
  39. :           // Deconstructor
  40. :           ~Address();
  41. :           // Operator overloads
  42. :         **************************************************************************
  43. :         *                                             *
  44. :         * friend ostream& operator <<(ostream& OutPutStream,Address& AnAddress); *
  45. :         *                                             *
  46. :         **************************************************************************
  47. :           // Functions
  48. :           void Change(int ANumber,const char *AStreet,const char *ACity,int AZip);
  49. : };
  50. : #endif
  51. : // address.cpp
  52. : #include "address.h"
  53. : #include <string.h>
  54. : // Constructors
  55. : Address::Address(int ANumber,const char *AStreet,const char *ACity,int AZip)
  56. : {
  57. :      Number=ANumber;
  58. :      Street=new char[strlen(AStreet)+1];
  59. :      strcpy(Street,AStreet);
  60. :      City=new char[strlen(ACity)+1];
  61. :      strcpy(City,ACity);
  62. :      Zip=AZip;
  63. : }
  64. : Address::Address()
  65. : {
  66. :      Number=0;
  67. :      Street=new char[strlen("")+1];
  68. :      strcpy(Street,"");
  69. :      City=new char[strlen("")+1];
  70. :      strcpy(City,"");
  71. :      Zip=0;
  72. : }
  73. : // Copy constructor
  74. : Address::Address(Address& AnAddress)
  75. : {
  76. :      Number=AnAddress.Number;
  77. :      Street=new char[strlen(AnAddress.Street)+1];
  78. :      strcpy(Street,AnAddress.Street);
  79. :      City=new char[strlen(AnAddress.City)+1];
  80. :      strcpy(City,AnAddress.City);
  81. :      Zip=AnAddress.Zip;
  82. : }
  83. : // Deconstructor
  84. : Address::~Address()
  85. : {
  86. :      Number=0;
  87. :      strcpy(Street,"");
  88. :      strcpy(City,"");
  89. :      Zip=0;
  90. : }
  91. : // Operator overloads
  92. : ***************************************************************
  93. :                                   *
  94. : ostream& operator <<(ostream& OutPutStream,Address& AnAddress)*
  95. :                                   *
  96. : ***************************************************************
  97. : {
  98. :      OutPutStream<<"Street : "<<AnAddress.Number<<" "<<AnAddress.Street<<EOLN;
  99. :      OutPutStream<<"City : "<<AnAddress.City<<EOLN;
  100. :      OutPutStream<<"Zip code : "<<AnAddress.Zip<<EOLN;
  101. : }
  102. : // Functions
  103. : void Address::Change(int ANumber,const char *AStreet,const char *ACity,int AZip)
  104. : {
  105. :      Number=ANumber;
  106. :      delete Street;
  107. :      Street=new char[strlen(AStreet)+1];
  108. :      strcpy(Street,AStreet);
  109. :      delete City;
  110. :      City=new char[strlen(ACity)+1];
  111. :      strcpy(City,ACity);
  112. :      Zip=AZip;
  113. : }
  114.